package com.itheima.web.service.impl.sms;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.result.PageResult;
import com.itheima.service.sms.SmsHomeRecommendProductService;
import com.itheima.vo.sms.SmsHomeRecommendProduct;
import com.itheima.web.mapper.sms.SmsHomeRecommendProductMapper;
import org.apache.dubbo.config.annotation.DubboService;

import java.util.List;

@DubboService
public class SmsHomeRecommendProductServiceImpl extends ServiceImpl<SmsHomeRecommendProductMapper, SmsHomeRecommendProduct> implements SmsHomeRecommendProductService {
    @Override
    public PageResult listPage(String productName, Integer pageNum, Integer pageSize, Integer recommendStatus) {
        Page<SmsHomeRecommendProduct> pageInfo = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<SmsHomeRecommendProduct> lqw = new LambdaQueryWrapper<>();
        lqw.like(productName != null, SmsHomeRecommendProduct::getProductName, productName);
        lqw.eq(recommendStatus != null, SmsHomeRecommendProduct::getRecommendStatus, recommendStatus);
        this.page(pageInfo, lqw);
        return PageResult.init(pageInfo.getTotal(), Long.valueOf(pageSize), Long.valueOf(pageNum), pageInfo.getRecords());

    }

    @Override
    public void updateRecommendStatus(List<Long> ids, Integer recommendStatus) {
        LambdaUpdateWrapper<SmsHomeRecommendProduct> luw = new LambdaUpdateWrapper<>();
        luw.set(SmsHomeRecommendProduct::getRecommendStatus, recommendStatus).in(SmsHomeRecommendProduct::getId, ids);
        this.update(luw);
    }

    @Override
    public void updateSort(Long id, Integer sort) {
        LambdaUpdateWrapper<SmsHomeRecommendProduct> luw = new LambdaUpdateWrapper<>();
        luw.eq(SmsHomeRecommendProduct::getId, id).set(SmsHomeRecommendProduct::getSort, sort);
        this.update(luw);
    }
}
